package com.yanchangapp.ui.activity.mail;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.MailType;
import com.yanchangapp.model.bean.MailAtmentBean;
import com.yanchangapp.model.bean.MailUserBean;
import com.yanchangapp.model.response.MailDetailResponse;
import com.yanchangapp.ui.presenter.MailDetailAtPresenter;
import com.yanchangapp.ui.view.MailDetailAtView;
import com.yanchangapp.util.WebviewHelper;
import com.yanchangapp.widget.BottomDialog;
import com.zxl.baselib.util.window.DisplayHelper;
import com.zxl.baselib.widget.TipDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 邮件详情页面
 * <p>
 * 想做一个加载的效果勒...
 * <p>
 * 转发回复时保持原始的样子
 * 详情的时候进行缩放的
 * 暂时这里的富文本是有问题的感觉...
 *
 */
public class MailDetailActivity extends BaseActivity<MailDetailAtView, MailDetailAtPresenter> implements MailDetailAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tvTheme)
    TextView tvTheme;
    @BindView(R.id.tvTextSingleName)
    TextView tvTextSingleName;
    @BindView(R.id.tvSendName)
    TextView tvSendName;
    @BindView(R.id.llOpen)
    LinearLayout llOpen;
    @BindView(R.id.llClose)
    LinearLayout llClose;
    @BindView(R.id.ivReply)
    ImageView ivReply;
    @BindView(R.id.ivDelete)
    ImageView ivDelete;
    @BindView(R.id.rvReceivers)
    RecyclerView rvReceivers;
    @BindView(R.id.rvSenders)
    RecyclerView rvCopies;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.llDetailPersons)
    LinearLayout llDetailPersons;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.llProgressBar)
    LinearLayout llProgressBar;
    @BindView(R.id.rvAttachmentLists)
    RecyclerView mRvAttachmentList;
    @BindView(R.id.webView)
    WebView mWebView;
    private TipDialog mActualDeleteDialog;
    private boolean isFirstIn = true;
    private int contentWidth;

    private static final String MAIL_ID = "mail_id";
    private static final String MAIL_TYPE_STRING = "mail_type_string";
    private static final String IS_FROM_SEARCH = "is_from_search";
    private int mMailID = -1;
    private String mMailTypeString = "";
    private boolean isFromSearch = false;
    private List<MailUserBean> receiveLists = new ArrayList<>();
    private List<MailUserBean> copyLists = new ArrayList<>();
    private List<MailAtmentBean> attachmentsList = new ArrayList<>();
    private BaseQuickAdapter<MailUserBean, BaseViewHolder> mReceiveAdapter;
    private BaseQuickAdapter<MailUserBean, BaseViewHolder> mCopyAdapter;
    private BaseQuickAdapter<MailAtmentBean,BaseViewHolder> mAttachmentAdapter;
    private BottomDialog mBottomChoice;
    private String mWebContent;
    private String mTheme;
    private String sendNickName;
    private String sendMailAddress;
    private String mMailTime;

    public static void show(Activity context, int mailID,String mailTypeString) {
        Intent intent = new Intent(context, MailDetailActivity.class);
        intent.putExtra(MAIL_ID, mailID);
        intent.putExtra(MAIL_TYPE_STRING,mailTypeString);
        context.startActivity(intent);
    }

    public static void show(Activity context, int mailID,String mailTypeString,boolean isFromSearch) {
        Intent intent = new Intent(context, MailDetailActivity.class);
        intent.putExtra(MAIL_ID, mailID);
        intent.putExtra(MAIL_TYPE_STRING,mailTypeString);
        intent.putExtra(IS_FROM_SEARCH,isFromSearch);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        if (getIntent() != null) {
            mMailID = getIntent().getIntExtra(MAIL_ID, -1);
            mMailTypeString = getIntent().getStringExtra(MAIL_TYPE_STRING);
            if (getIntent().hasExtra(IS_FROM_SEARCH)){
                isFromSearch = getIntent().getBooleanExtra(IS_FROM_SEARCH,false);
            }

        }else {
            finish();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_mail_detail;
    }

    @Override
    protected MailDetailAtPresenter createPresenter() {
        return new MailDetailAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        showLoading();
        tvToolbarTitle.setText("邮件详情");
        initWebView(mWebView);
        initRvAdapter();
    }

    /**
     * 显示加载状态
     */
    private void showLoading(){
        if (llContent.getVisibility() == View.VISIBLE){
            llContent.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            llErrorNetwork.setVisibility(View.GONE);
        }

        if (llProgressBar.getVisibility() == View.GONE){
            llProgressBar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 显示完成状态
     */
    private void showContentView(){
       if (llContent.getVisibility() == View.GONE){
           llContent.setVisibility(View.VISIBLE);
       }

       if (llErrorNetwork.getVisibility() == View.VISIBLE){
           llErrorNetwork.setVisibility(View.GONE);
       }

       if (llProgressBar.getVisibility() == View.VISIBLE){
           llProgressBar.setVisibility(View.GONE);
       }
    }

    /**
     * 显示错误状态
     */
    private void showErrorContent(){
        if (llContent.getVisibility() == View.VISIBLE){
            llContent.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.GONE){
            llErrorNetwork.setVisibility(View.VISIBLE);
        }

        if (llProgressBar.getVisibility() == View.VISIBLE){
            llProgressBar.setVisibility(View.GONE);
        }
    }


    // 初始化rvAdapter
    private void initRvAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        rvReceivers.setHasFixedSize(true);
        rvReceivers.setNestedScrollingEnabled(false);
        rvCopies.setHasFixedSize(true);
        rvCopies.setNestedScrollingEnabled(false);
        rvReceivers.setLayoutManager(manager);
        mReceiveAdapter = new BaseQuickAdapter<MailUserBean, BaseViewHolder>(R.layout.layout_just_show_text, receiveLists) {
            @Override
            protected void convert(BaseViewHolder helper,MailUserBean item) {
                helper.setText(R.id.tvUserName, getString(R.string.user_mail, item.getNickname(), item.getAddress()));
            }
        };

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);
        rvCopies.setLayoutManager(linearLayoutManager);
        mCopyAdapter = new BaseQuickAdapter<MailUserBean, BaseViewHolder>(R.layout.layout_just_show_text, copyLists) {
            @Override
            protected void convert(BaseViewHolder helper, MailUserBean item) {
                helper.setText(R.id.tvUserName, getString(R.string.user_mail, item.getNickname(), item.getAddress()));
            }
        };
        rvReceivers.setAdapter(mReceiveAdapter);
        rvCopies.setAdapter(mCopyAdapter);


        LinearLayoutManager attachmentManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        attachmentManager.setSmoothScrollbarEnabled(true);
        attachmentManager.setAutoMeasureEnabled(true);
        mRvAttachmentList.setLayoutManager(attachmentManager);
        mAttachmentAdapter = new BaseQuickAdapter<MailAtmentBean, BaseViewHolder>(R.layout.item_attachment_show,attachmentsList) {
            @Override
            protected void convert(BaseViewHolder helper, MailAtmentBean item) {
                // 设置附件名
                helper.setText(R.id.tvAttachment,item.getName());
                helper.addOnClickListener(R.id.tvLook);
            }
        };
        mAttachmentAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            // 点击附件 进行查看工作...
            if (view.getId() == R.id.tvLook){
               AttachmentShowActivity.show(MailDetailActivity.this,attachmentsList.get(position).getPath());
            }
        });
        mRvAttachmentList.setAdapter(mAttachmentAdapter);

    }

    private void showBottomChooseDialog() {
        if (mBottomChoice == null) {
            mBottomChoice = new BottomDialog(this, 0, true);
            View view = LayoutInflater.from(this).inflate(R.layout.bottom_layout_detail_reply, null, false);
            mBottomChoice.setContentView(view);
            // 设置背景为透明色 那么白色的就能呈现出来了
            mBottomChoice.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                    .setBackgroundColor(getResources().getColor(android.R.color.transparent));
            view.findViewById(R.id.llReplyOne).setOnClickListener(v -> {
                mBottomChoice.hide();
                // 回复一人
                // 暂无问题...
                List<MailAtmentBean> mailAtmentBeans = new ArrayList<>();
                // 发送人列表
                List<MailUserBean> mailUserBeanList = new ArrayList<>();
                // 检验进行拼接...
                StringBuilder stringBuffer = new StringBuilder();
                stringBuffer.append("------------原始邮件-----------<br/>");
                stringBuffer.append("发件人：").append(sendNickName).append("<br/>");
                stringBuffer.append("时间：").append(mMailTime).append("<br/>");
                stringBuffer.append("收件人：");
                for (int i=0;i<receiveLists.size();i++){
                    MailUserBean mailUserBean = receiveLists.get(i);
                    String style = "60px";
                    if (i == 0){
                        style = "0px";
                    }
                    stringBuffer.append("<span style=\'margin-left:").append(style).append("\'>")
                            .append(mailUserBean.getNickname()).append("&lt;").append(mailUserBean.getAddress()).append("&gt;").append("</span><br/>");
                }
                if (receiveLists.size() == 0){
                    stringBuffer.append("<br/>");
                }
                stringBuffer.append("抄送人：");
                for (int i=0;i<copyLists.size();i++){
                    MailUserBean mailUserBean = copyLists.get(i);
                    String style = "60px";
                    if (i == 0){
                        style = "0px";
                    }
                    stringBuffer.append("<span style=\'margin-left:").append(style).append("\'>")
                            .append(mailUserBean.getNickname()).append("&lt;").append(mailUserBean.getAddress()).append("&gt;").append("</span><br/>");
                }
                if (copyLists.size() == 0){
                    stringBuffer.append("<br/>");
                }
                // 添加邮件主题
                stringBuffer.append("主题：").append(mTheme).append("<br/>");
                // 添加内容...
                stringBuffer.append("内容：").append("<br/>").append(mWebContent);

                if (!TextUtils.isEmpty(sendMailAddress)){
                    String[] splitString = sendMailAddress.split("@");
                    mailUserBeanList.add(new MailUserBean(sendMailAddress,sendNickName,splitString[0]));
                }
                // 抄送人列表
                List<MailUserBean> ccUserBeanList = new ArrayList<>();
                String theme = "回复: "+mTheme;
                TranspondActivity.jumpFromDetail(
                        MailDetailActivity.this,
                        TranspondActivity.TITLE_TRANS_ONE,
                        mailUserBeanList,
                        ccUserBeanList,
                        theme,
                        stringBuffer.toString(),
                        mailAtmentBeans,
                        isFromSearch
                );
            });

            view.findViewById(R.id.llReplyAll).setOnClickListener(v -> {
                mBottomChoice.hide();
                // UIUtils.showToast("回复全部人");
                List<MailAtmentBean> mailAtmentBeans = new ArrayList<>();
                List<MailUserBean> mailUserBeanList = new ArrayList<>();
                // 检验进行拼接...
                if (!TextUtils.isEmpty(sendMailAddress)){
                    String[] splitString = sendMailAddress.split("@");
                    mailUserBeanList.add(new MailUserBean(sendMailAddress,sendNickName,splitString[0]));
                }

                for (int i = 0;i<receiveLists.size();i++){
                    String address = receiveLists.get(i).getAddress();
                    MailUserBean userBean = receiveLists.get(i);
                    if (!address.equals(sendMailAddress)){
                        mailUserBeanList.add(new MailUserBean(userBean.getAddress(),userBean.getNickname(),userBean.getUsername()));
                    }
                }

                String theme = "回复: "+mTheme;
                StringBuilder stringBuffer = new StringBuilder();
                stringBuffer.append("------------原始邮件-----------<br/>");
                stringBuffer.append("发件人：").append(sendNickName).append("<br/>");
                stringBuffer.append("时间：").append(mMailTime).append("<br/>");
                stringBuffer.append("收件人：");
                for (int i=0;i<receiveLists.size();i++){
                    MailUserBean mailUserBean = receiveLists.get(i);
                    String style = "60px";
                    if (i == 0){
                        style = "0px";
                    }
                    stringBuffer.append("<span style=\'margin-left:").append(style).append("\'>")
                            .append(mailUserBean.getNickname()).append("&lt;").append(mailUserBean.getAddress()).append("&gt;").append("</span><br/>");
                }
                if (receiveLists.size() == 0){
                    stringBuffer.append("<br/>");
                }
                stringBuffer.append("抄送人：");
                for (int i=0;i<copyLists.size();i++){
                    MailUserBean mailUserBean = copyLists.get(i);
                    String style = "60px";
                    if (i == 0){
                        style = "0px";
                    }
                    stringBuffer.append("<span style=\'margin-left:").append(style).append("\'>")
                            .append(mailUserBean.getNickname()).append("&lt;").append(mailUserBean.getAddress()).append("&gt;").append("</span><br/>");
                }
                if (copyLists.size() == 0){
                    stringBuffer.append("<br/>");
                }
                // 添加邮件主题
                stringBuffer.append("主题：").append(mTheme).append("<br/>");
                // 添加内容...
                stringBuffer.append("内容：").append("<br/>").append(mWebContent);
                TranspondActivity.jumpFromDetail(
                        MailDetailActivity.this,
                        TranspondActivity.TITLE_TRANS_ALL,
                        mailUserBeanList,
                        copyLists,
                        theme,
                        stringBuffer.toString(),
                        mailAtmentBeans,
                        isFromSearch
                );
            });

            view.findViewById(R.id.llTransPond).setOnClickListener(v -> {
                mBottomChoice.hide();
                // 转发都携带
                String theme = "转发: "+mTheme;
                List<MailUserBean> mailUserBeanList = new ArrayList<>();
                StringBuilder stringBuffer = new StringBuilder();
                stringBuffer.append("------------原始邮件-----------<br/>");
                stringBuffer.append("发件人：").append(sendNickName).append("<br/>");
                stringBuffer.append("时间：").append(mMailTime).append("<br/>");
                stringBuffer.append("收件人：");
                for (int i=0;i<receiveLists.size();i++){
                    MailUserBean mailUserBean = receiveLists.get(i);
                    String style = "60px";
                    if (i == 0){
                        style = "0px";
                    }
                    stringBuffer.append("<span style=\'margin-left:").append(style).append("\'>")
                            .append(mailUserBean.getNickname()).append("&lt;").append(mailUserBean.getAddress()).append("&gt;").append("</span><br/>");
                }
                if (receiveLists.size() == 0){
                    stringBuffer.append("<br/>");
                }
                stringBuffer.append("抄送人：");
                for (int i=0;i<copyLists.size();i++){
                    MailUserBean mailUserBean = copyLists.get(i);
                    String style = "60px";
                    if (i == 0){
                        style = "0px";
                    }
                    stringBuffer.append("<span style=\'margin-left:").append(style).append("\'>")
                            .append(mailUserBean.getNickname()).append("&lt;").append(mailUserBean.getAddress()).append("&gt;").append("</span><br/>");
                }
                if (copyLists.size() == 0){
                    stringBuffer.append("<br/>");
                }
                // 添加邮件主题
                stringBuffer.append("主题：").append(mTheme).append("<br/>");
                // 添加内容...
                stringBuffer.append("内容：").append("<br/>").append(mWebContent);
                TranspondActivity.jumpFromDetail(
                        MailDetailActivity.this,
                        TranspondActivity.TITLE_ZHUAN_FA,
                        mailUserBeanList,
                        mailUserBeanList,
                        theme,
                        stringBuffer.toString(),
                        attachmentsList,
                        isFromSearch
                );
            });
        }
        mBottomChoice.show();
    }



    private void showDeleteDialog() {
        if (mActualDeleteDialog == null){
            mActualDeleteDialog = new TipDialog.Builder(this)
                    .setTitle("彻底删除")
                    .setMessage("彻底删除后,邮件将不可恢复")
                    .setEnsureText("确定删除")
                    .setEnsureClickListener(new TipDialog.EnsureClickListener() {
                        @Override
                        public void onEnsureClick(TipDialog tipDialog) {
                            mActualDeleteDialog.dismiss();
                            // 执行确认删除逻辑
                            mPresenter.actualDelete(mMailID);
                        }
                    }).build();
        }
        mActualDeleteDialog.show();
    }

    @Override
    protected void initData() {
        // 进行数据请求...
        if (mMailID != -1) {
            mPresenter.queryMailDetailInfo(mMailID);
        }
    }

    @Override
    protected void initListener() {
        llOpen.setOnClickListener(v -> {
            llOpen.setVisibility(View.GONE);
            llClose.setVisibility(View.VISIBLE);
            llDetailPersons.setVisibility(View.VISIBLE);

        });

        llClose.setOnClickListener(v -> {
            llClose.setVisibility(View.GONE);
            llOpen.setVisibility(View.VISIBLE);
            llDetailPersons.setVisibility(View.GONE);
        });

        ivToolbarNavigation.setOnClickListener(v -> finish());

        ivReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomChooseDialog();
            }
        });

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleDeleteAction();
            }
        });

        btnNextRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoading();
                if (mMailID != -1) {
                    mPresenter.queryMailDetailInfo(mMailID);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstIn){
            isFirstIn = false;
        }else {
            mWebView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.getSettings().setLightTouchEnabled(false);
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null){
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();
            ViewGroup parent = (ViewGroup) mWebView.getParent();
            if (parent != null) {
                parent.removeView(mWebView);
            }
            mWebView.removeAllViews();
            mWebView.stopLoading();
            mWebView.setWebChromeClient(null);
            mWebView.setWebViewClient(null);
            mWebView.destroy();
        }

        if (mBottomChoice != null){
            mBottomChoice.dismiss();
        }
        super.onDestroy();
    }

    /**
     * 进行删除操作...
     */
    private void handleDeleteAction() {
        if (mMailTypeString.equals(MailType.TRASH.getMailDetail().getDescription()) ||
        mMailTypeString.equals(MailType.SPAM.getMailDetail().getDescription())){
            showDeleteDialog();
        }else {
            mPresenter.moveToDelete(mMailID);
        }
    }

    private void initWebView(WebView webView){
        // 隐藏滑动
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        WebviewHelper.initWebView(webView);
        webView.setOnTouchListener(new View.OnTouchListener() {
            private float startx;
            private float starty;
            private float offsetx;
            private float offsety;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        startx = event.getX();
                        starty = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        offsetx = Math.abs(event.getX() - startx);
                        offsety = Math.abs(event.getY() - starty);
                        if (offsetx > offsety) {
                            v.getParent().requestDisallowInterceptTouchEvent(true);
                        } else {
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
    }

    private void fillWebviewBody(WebView webview,String content){
        StringBuilder body = new StringBuilder();
        body.append(WebviewHelper.getWebViewStartString());
        // 添加后缀的逻辑....
        double initScale = 1;
        double windowWidth = (DisplayHelper.getScreenWidth(this) - 16)/DisplayHelper.getDensity(this);
        Log.e("detail", "fillWebviewBody: "+ windowWidth);
        if (contentWidth != 0){
            Log.e("detail",""+contentWidth);
            if (windowWidth < contentWidth){
                initScale = windowWidth / contentWidth;
            }
        }
        body.append(getString(R.string.webView_scale_xml,String.valueOf(initScale)));
        body.append(WebviewHelper.getWebViewStart());
        body.append(WebviewHelper.normalViewBodyString());
        body.append(content);
        body.append(WebviewHelper.getWebViewEndString());
        webview.loadDataWithBaseURL(null, body.toString(), "text/html",
                "utf-8", null);
    }

    @Override
    public void queryMailDetailFailure(String err) {
        UIUtils.showToast(err);
        showErrorContent();
    }

    @Override
    public void queryMailDetailSuccess(MailDetailResponse.DataBean dataBean) {
        showContentView();
        if (null != dataBean) {
            contentWidth = dataBean.getContentWidth();
            tvSendName.setText(getString(R.string.user_mail, dataBean.getSenderNickname(), dataBean.getSenderAddress()));
            if (TextUtils.isEmpty(dataBean.getSenderNickname())) {
                tvTextSingleName.setText("");
            } else {
                tvTextSingleName.setText(dataBean.getSenderNickname().substring(0, 1));
                sendMailAddress = dataBean.getSenderAddress();
                sendNickName = dataBean.getSenderNickname();
            }
            // 设置时间...
            tvTime.setText(dataBean.getMailTime());
            mMailTime = dataBean.getCreateTime();
            if (TextUtils.isEmpty(dataBean.getSubject())) {
                tvTheme.setText("(无主题)");
            } else {
                mTheme = dataBean.getSubject();
                tvTheme.setText(dataBean.getSubject());
            }

            if (dataBean.getToUsers() != null && dataBean.getToUsers().size() != 0) {
                receiveLists.clear();
                receiveLists.addAll(dataBean.getToUsers());
                mReceiveAdapter.notifyDataSetChanged();
            }

            if (dataBean.getCcUsers() != null && dataBean.getCcUsers().size() != 0) {
                copyLists.clear();
                copyLists.addAll(dataBean.getCcUsers());
                mCopyAdapter.notifyDataSetChanged();
            }

            if (dataBean.getAttachments() != null && dataBean.getAttachments().size() != 0){
                attachmentsList.clear();
                attachmentsList.addAll(dataBean.getAttachments());
                mAttachmentAdapter.notifyDataSetChanged();
            }

            if (dataBean.getContent() != null){
                mWebContent = dataBean.getContent();
                fillWebviewBody(mWebView,dataBean.getContent());
            }
        }
    }

    @Override
    public void deleteSuccess() {
        // 进行通知
        mRxManager.post(AppConstant.NOTIFY_MAIL_DELETE_SUCCESS,true);
        // 如果从详情页进来,则删除
        if (isFromSearch){
            mRxManager.post(AppConstant.NOTIFY_SEARCH_DELETE_SUCCESS,true);
        }
        finish();
    }

    @Override
    public void deleteFailure(String err) {
        UIUtils.showToast(err);
    }
}
